package apis

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/go-admin-team/go-admin-core/sdk/api"
	_ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
	"vrcm/src/vrcm/app/admin/models/monitor"
	"vrcm/src/vrcm/app/admin/service/dto/monitor"
	"vrcm/src/vrcm/app/admin/service/monitor"
	"vrcm/src/vrcm/common/apis"
)

type MonitorBasic struct {
	api.Api
}

// GetPage 获取MonitorBasic列表
// @Summary 获取MonitorBasic列表
// @Description 获取MonitorBasic列表
// @Tags MonitorBasic
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} response.Response{data=response.Page{list=[]models.MonitorBasic}} "{"code": 200, "data": [...]}"
// @Router /api/v1/monitor-basic [get]
// @Security Bearer
func (e MonitorBasic) GetPage(c *gin.Context) {
	req := dto.MonitorBasicGetPageReq{}
	s := service.MonitorBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	list := make([]monitor.MonitorBasic, 0)
	var count int64

	err = s.GetPage(&req, &list, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取MonitorBasic失败，失败信息 %s", err.Error()))
		return
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Get 获取MonitorBasic
// @Summary 获取MonitorBasic
// @Description 获取MonitorBasic
// @Tags MonitorBasic
// @Param id path int false "id"
// @Success 200 {object} response.Response{data=models.MonitorBasic} "{"code": 200, "data": [...]}"
// @Router /api/v1/monitor-basic/{id} [get]
// @Security Bearer
func (e MonitorBasic) Get(c *gin.Context) {
	req := dto.MonitorBasicGetReq{}
	s := service.MonitorBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.Get(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取MonitorBasic失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(data, "查询成功")
}

// Insert 创建MonitorBasic
// @Summary 创建MonitorBasic
// @Description 创建MonitorBasic
// @Tags MonitorBasic
// @Accept application/json
// @Product application/json
// @Param data body dto.MonitorBasicInsertReq true "data"
// @Success 200 {object} response.Response	"{"code": 200, "message": "添加成功"}"
// @Router /api/v1/monitor-basic [post]
// @Security Bearer
func (e MonitorBasic) Insert(c *gin.Context) {
	req := dto.MonitorBasicInsertReq{}
	s := service.MonitorBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.Insert(&req)
	if err != nil {
		e.Error(apis.DBCreateError.Code, err, fmt.Sprintf("创建MonitorBasic失败， %s", err.Error()))
		return
	}

	e.OK(req.GetId(), "创建成功")
}

func (e MonitorBasic) SaveMonitor(c *gin.Context) {
	req := dto.MonitorBasicUpdateReq{}
	s := service.MonitorBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	if req.MonitorBasic.TemplateType <= 0 {
		e.Error(apis.TemplateIsEmptyError.Code, err, fmt.Sprintf("修改MonitorBasic失败，失败信息 %s", "监看模版为空"))
		return
	}
	err = s.SaveMonitor(&req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("修改MonitorBasic失败，失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "修改成功")
}

func (e MonitorBasic) SaveMonitorConfig(c *gin.Context) {
	req := dto.MonitorBasicConfigReq{}
	s := service.MonitorBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.SaveMonitorConfig(&req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("修改MonitorBasic失败，失败信息 %s", err.Error()))
		return
	}
	e.OK(req.Id, "修改成功")
}

func (e MonitorBasic) GetMonitorConfig(c *gin.Context) {
	req := dto.MonitorConfigGetReq{}
	s := service.MonitorBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetMonitorConfig(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("查询监看失败，失败信息 %s", err.Error()))
		return
	}
	e.OK(data, "查询成功")
}

func (e MonitorBasic) Delete(c *gin.Context) {
	s := service.MonitorBasic{}
	req := dto.MonitorBasicDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.Remove(&req)
	if err != nil {
		e.Error(apis.DBDeleteError.Code, err, fmt.Sprintf("删除MonitorBasic失败，失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "删除成功")
}

func (e MonitorBasic) StartMonitor(c *gin.Context) {
	s := service.MonitorBasic{}
	req := dto.MonitorStartReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.StartMonitor(&req)
	if err != nil {
		e.Error(apis.StartMonitorError.Code, err, fmt.Sprintf("开启监看任务失败，失败信息 %s", err.Error()))
		return
	}
	e.OK("", "开启监看任务成功")
}

func (e MonitorBasic) StopMonitor(c *gin.Context) {
	s := service.MonitorBasic{}
	req := dto.MonitorStopReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.StopMonitor(&req)
	if err != nil {
		e.Error(apis.StopMonitorError.Code, err, fmt.Sprintf("停止监看任失败，失败信息 %s", err.Error()))
		return
	}
	e.OK("", "停止监看任成功")
}

func (e MonitorBasic) ResetMonitorConfig(c *gin.Context) {
	s := service.MonitorBasic{}
	req := dto.RestReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.ResetMonitorConfig(&req)
	if err != nil {
		e.Error(apis.StopMonitorError.Code, err, fmt.Sprintf("重制监看任失败，失败信息 %s", err.Error()))
		return
	}
	e.OK("", "重制监看任成功")
}

func (e MonitorBasic) CreateShare(c *gin.Context) {
	s := service.MonitorBasic{}
	req := dto.CreateShareReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.CreateShare(&req)
	if err != nil {
		e.Error(apis.StopMonitorError.Code, err, fmt.Sprintf("创建分享失败，失败信息 %s", err.Error()))
		return
	}
	e.OK("", "创建分享成功")
}

func (e MonitorBasic) GetShareInfoByUuid(c *gin.Context) {
	s := service.MonitorBasic{}
	req := dto.GetShareInfoReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	basic, err := s.GetShareInfoByUuid(&req)
	if err != nil {
		e.Error(apis.CreateMonitorShareError.Code, err, fmt.Sprintf("获取分享失败，失败信息 %s", err.Error()))
		return
	}
	e.OK(basic, "获取分享成功")
}

func (e MonitorBasic) GetByEventId(c *gin.Context) {
	req := dto.MonitorBasicGetByEventIdReq{}
	s := service.MonitorBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetByEventId(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取MonitorBasic失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(data, "查询成功")
}
